package shajie.shajun.mmhf.View.FloatView;


import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import shajie.shajun.mmhf.Public_Data;
import shajie.shajun.mmhf.R;
import shajie.shajun.mmhf.View.MainActivity;


/**
 * Created by MarioStudio on 2016/5/23.
 */

public class FloatService extends Service implements  View.OnClickListener{
    private FloatWindow floatWindow;
    private View menuView, floatView;

    private TextView tv_home,tv_searchorder,tv_productdetail,tv_recentorder;
    @Override
    public IBinder onBind(Intent intent) {
        return new MusicBinder();
    }
    public class MusicBinder extends Binder {
        public FloatService getService() {
            return FloatService.this;
        }
    }
    @Override
    public void onCreate() {
        super.onCreate();
        initFloatWindow();
    }
    private void initFloatWindow() {
        floatView = LayoutInflater.from(this).inflate(R.layout.layout_float, null);
        menuView = LayoutInflater.from(this).inflate(R.layout.layout_menu, null);
        tv_home=(TextView)menuView.findViewById(R.id.tv_home);
        tv_home.setOnClickListener(this);
        tv_recentorder=(TextView)menuView.findViewById(R.id.tv_recentorder);
        tv_recentorder.setOnClickListener(this);
        tv_searchorder=(TextView)menuView.findViewById(R.id.tv_ordersearch);
        tv_searchorder.setOnClickListener(this);
        tv_productdetail=(TextView)menuView.findViewById(R.id.tv_productdetail);
        tv_productdetail.setOnClickListener(this);
        floatWindow = new FloatWindow(this);
        floatWindow.setFloatView(floatView);
        floatWindow.setPlayerView(menuView);
    }

    /**
     * 打开悬浮窗
     * */
    public void show() {
        if(null != floatWindow) {
            floatWindow.show();
        }
    }
    /**
     * 关闭悬浮窗
     * */
    public void dismiss() {
        if(null != floatWindow) {
            floatWindow.dismiss();
        }
    }
    @Override
    public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_home:
                    Public_Data.type=0;
                    Intent intent=new Intent(this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    this.startActivity(intent);
                    floatWindow.turnMini();
                    dismiss();
                    break;
                case R.id.tv_ordersearch:
                    Public_Data.type=1;
                    Intent intent2=new Intent(this, MainActivity.class);
                    intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    this.startActivity(intent2);
                    floatWindow.turnMini();
                    dismiss();
                    break;
                case R.id.tv_productdetail:
                    Public_Data.type=2;
                    Intent intent3=new Intent(this, MainActivity.class);
                    intent3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    this.startActivity(intent3);
                    floatWindow.turnMini();
                    dismiss();
                    break;
                case R.id.tv_recentorder:
                    Public_Data.type=3;
                    Intent intent4=new Intent(this, MainActivity.class);
                    intent4.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    this.startActivity(intent4);
                    floatWindow.turnMini();
                    dismiss();
                    break;
                default:
                    break;
            }
        }
}
